import commJS from '../common.js';

export default {
  components: {},
  data() {
    return {
      //  表单
      form_data: {
        name: '',
        uid: '',
        code: '',
      },
      restaurants: [],
      kw: "",
      id:'',
      rules: {
        name: [{
          required: true,
          message: '请选择微信用户名',
          trigger: 'blur',
        }],
      },
      chars:'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
      smallLesson_id:'',
      smallLesson_title:''
    }
  },

  /**
   * 进入页面加载
   */
  mounted: function () {
    var that = this;

    var query = that.$route.query;
    if (query.smallLesson_id) {
      that.smallLesson_id = query.smallLesson_id;
      that.smallLesson_title=query.smallLesson_name;
      if(query.id){
        that.id=query.id;
        that.getDetail();
      }

    }
    that.getTitle();
    this.getcode(10);
  },

  //方法
  methods: {
    /*
    * 服务器获取标题
    * */
    querySearchAsync(queryString, cb) {
      // if(!this.form_data.type)return this.$message.error("请先选择推送类型");
      var that = this;
      that.axios.post("/user/index", {
        key: this.form_data.name,
        page: 1,
        limit: 20
      }).then(data => {
        this.restaurants = [];
        this.restaurants = data.list;
        var restaurants = that.restaurants;
        var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          cb(results);
        }, 1000 * Math.random());
      });
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.nickname.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    getcode(len) {
      len = len || 32;
      var maxPos = this.chars.length;
      var code = '';
      for (var i = 0; i < len; i++) {
        //0~61的整数
        var randomNum = this.generateRandomNum(0, maxPos - 1);
        var char = this.chars.charAt(randomNum);
        // console.log(char);
        code += char;
      }
      this.form_data.code=code;
    },
    generateRandomNum(min, max) {
      if (max === undefined || min === undefined) {
        return false;
      }
      let num = Math.floor(Math.random()*(max-min+1)+min);
      return num;
    },
    /*得到当前标题*/
    handleSelect(item) {
      this.$set(this.form_data, "name", item.nickname)
      this.$set(this.form_data, "uid", item.id)
    },
    /**
     * 获取详情
     */
    getDetail() {
      var that = this;

      that.axios.post("/ebookCode/edit", {
        id: that.id
      }).then(data => {
        if (data) {
          that.form_data.name = data.name;
          that.form_data.uid = data.uid;
          that.form_data.code = data.code;
        }
      });
    },
    /*获取课程标题等*/
    getTitle() {
      var that = this;

      that.axios.post("/user/index", {
        key: this.form_data.name,
        page: 1,
        limit: 20
      }).then(data => {
        this.restaurants = [];
        this.restaurants = data.list;
        // if(data.status=="success"){
        //   this.restaurants=data.data
        // }else{
        //   this.$message(data.msg)
        // }
      });
    },
    /**
     * 保存预览
     */
    save() {
      const that = this;
      that.$refs.form_data.validate((valid) => {
        if (!valid) return that.$message.warning('请完整填写内容!');
        var formData = {};

        formData.eid = that.smallLesson_id;
        formData.uid = that.form_data.uid;
        formData.name = that.form_data.name;
        formData.code = that.form_data.code;
        var url = '/ebookCode/create';
        if (that.banner_id) {
          formData.id = that.banner_id;
          url = '/ebookCode/update';
        }
        that.axios.post(url, formData).then(() => {

          that.$message.success(that.banner_id ? '修改成功' : '添加成功');
          that.$router.go(-1)
        });
      })
    },
    /**
     * 返回
     */
    back() {
      this.$router.go(-1);
    },
  }
}
